package com.ovo.network.error;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ovo.network.R;
import com.ovo.network.error.impl.ReloadrListener;
import com.ovo.network.utils.RetrofitUtils;

/**
 * 网络错误重新加载
 */
public class ErrorConnetActivity extends Activity {

    private TextView tv_error_text;
    private TextView tv_reload;
    private TextView tv_network;

    private static ReloadrListener reloadrListener;

    private boolean no_network = false;

    public static void setreloadrListener(ReloadrListener listener){
        reloadrListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error_connet);

        no_network = getIntent().getBooleanExtra("no_network", false);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        tv_error_text = findViewById(R.id.tv_error_text);
        tv_reload = findViewById(R.id.tv_reload);
        tv_network = findViewById(R.id.tv_network);

        if (no_network){
            tv_reload.setVisibility(View.GONE);
            tv_network.setVisibility(View.VISIBLE);
            tv_error_text.setText(getString(R.string.connect_error));
        }else {
            tv_reload.setVisibility(View.VISIBLE);
            tv_network.setVisibility(View.GONE);
            tv_error_text.setText(getString(R.string.error_tip));
        }

        tv_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reloadrListener != null){
                    finish();
                    reloadrListener.onReload();
                }
            }
        });

        tv_network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RetrofitUtils.openSettingUI(ErrorConnetActivity.this);
                tv_reload.setVisibility(View.VISIBLE);
                tv_network.setVisibility(View.GONE);
                tv_error_text.setText(getString(R.string.error_tip));
            }
        });
    }
}
